﻿using System.Web.Security;
using Jayden.Dll.Core.Membership;
using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.Events;
using Jayden.Dll.Web.HtmlControls;

namespace Jayden.Dll.Web.Controls
{
	public class LogoutA : A
	{
		public delegate void OnLogout(HttpHandlerContext context);

		public class LogoutCommandHandler : HttpCommandHandler
		{
			public LogoutCommandHandler() : this("Logout") { }
			public LogoutCommandHandler(string command) : base(command)
			{
				RegisterEvent("Logout");
			}
			public event OnLogout Logout { add { AddEvent<OnLogout>("Logout", value); } remove { RemoveEvent<OnLogout>("Logout", value); } }

			protected override void  ProcessCommand(HttpHandlerContext context, string command)
			{
				FormsAuthentication.SignOut();
				Invoke("Logout", context);
			}
		}

		public override void Load(System.Xml.XmlNode node, HtmlControlLoader loader)
		{
			base.Load(node, loader);

			if (string.IsNullOrWhiteSpace(Id))
				Id = "logout";
		}

		private void AssignUri(HttpHandlerContext context)
		{
			Uri baseUri = new Uri(context.Context.Request.Url);
			Uri uri = new Uri(baseUri);
			uri["command"] = "Logout";
			HRef = uri.ToRelativeString(baseUri, false);
			if (!context.Context.User.Identity.IsAuthenticated)
				Classes.Add("notAuthenticated");
		}

		public override void ProcessHtmlRequest(HttpHandlerContext context)
		{
			LogoutCommandHandler logoutHandler = context.HandlerManager.GetHandler<LogoutCommandHandler>();
			logoutHandler.Logout += new OnLogout(Logout);
			LoginForm.LoginCommandHandler loginHandler = context.HandlerManager.GetHandler<LoginForm.LoginCommandHandler>();
			loginHandler.LoginSuccess += new LoginForm.OnLoginSuccess(LoginSuccess);
			AssignUri(context);
			base.ProcessHtmlRequest(context);
		}

		public override void ProcessJsonRequest(HttpHandlerContext context)
		{
			LogoutCommandHandler logoutHandler = context.HandlerManager.GetHandler<LogoutCommandHandler>();
			logoutHandler.Logout += new OnLogout(Logout);
			LoginForm.LoginCommandHandler loginHandler = context.HandlerManager.GetHandler<LoginForm.LoginCommandHandler>();
			loginHandler.LoginSuccess += new LoginForm.OnLoginSuccess(LoginSuccess);
			AssignUri(context);
			base.ProcessJsonRequest(context);
		}

		public override void ProcessJsRequest(HttpHandlerContext context)
		{
			if (string.IsNullOrWhiteSpace(Id))
				return;
			if (!context.Js.Contains(Id))
			{
				context.Js[Id].AppendAjaxHandler(@"
		if(data.login) {
			if(data.login.success) {
				$('#" + Id + @"').removeClass('notAuthenticated');
			}
		} else if(data.logout) {
			$('#" + Id + @"').addClass('notAuthenticated');
			alert('You are de-authenticated.');
			if(data.logoutHref)
				window.location.href = data.logoutHref;
		}
");
				context.Js[Id].AppendOnInit(@"
	$('#" + Id + @"').click(JAYDEN.sfn.clickAjax);
");
				base.ProcessJsRequest(context);
			}
		}

		private void LoginSuccess(HttpHandlerContext context, User user)
		{
			if (context.RequestType == HttpRequestType.Html)
				Classes.Remove("notAuthenticated");
		}

		private void Logout(HttpHandlerContext context)
		{
			if (context.RequestType == HttpRequestType.Json)
			{
				if(!context.Json.ContainsKey("logout"))
					context.Json.Add("logout", true);
			}
			else if (context.RequestType == HttpRequestType.Html)
			{
				Uri uri = new Uri(context.Request.Url);
				if (uri["command"] != null)
				{
					uri["command"] = null;
					context.Context.Response.Redirect(uri.ToAbsoluteString(true), true);
				}
				else
					Classes.Add("notAuthenticated");
			}
		}

	}
}
